package $ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

{table.package};
        {plugin.middlegen.appname}.model.${table.destinationClassName}Params;
        {plugin.middlegen.appname}.model.${table.destinationClassName};
        {plugin.middlegen.appname}.service.${table.destinationClassName}Service;

#set($endIndex = $plugin.middlegen.appname.lastIndexOf("."))
#set($jspIndex = $plugin.middlegen.appname.lastIndexOf(".")+1)
#set($jsppath = $plugin.middlegen.appname.substring($jspIndex))
#set($tmpStr = $plugin.middlegen.appname.substring(0,$endIndex))
#set($startIndex = $tmpStr.lastIndexOf(".")+1)
#set($module = $tmpStr.substring($startIndex,$endIndex))
#set($getStr = "list.get(i-1).get")

#set($JavaType = "")
#if($table.PkColumn.SqlTypeName == 'VARCHAR' || $table.PkColumn.SqlTypeName == 'CHAR' || $table.PkColumn.SqlTypeName == 'LONGVARCHAR')
#set($JavaType = "String")
#end
#if($table.PkColumn.SqlTypeName == 'INTEGER' || $table.PkColumn.SqlTypeName == 'INT')
#set($JavaType = "Integer")
#end
#if($table.PkColumn.SqlTypeName == 'BIT' || $table.PkColumn.SqlTypeName == 'BOOLEAN')
#set($JavaType = "Boolean")
#end
#if($table.PkColumn.SqlTypeName == 'TINYINT')
#set($JavaType = "Byte")
#end
#if($table.PkColumn.SqlTypeName == 'SMALLINT')
#set($JavaType = "Short")
#end
#if($table.PkColumn.SqlTypeName == 'BIGINT')
#set($JavaType = "Long")
#end
#if($table.PkColumn.SqlTypeName == 'REAL')
#set($JavaType = "Float")
#end
#if($table.PkColumn.SqlTypeName == 'FLOAT' || $table.PkColumn.SqlTypeName == 'DOUBLE')
#set($JavaType = "Double")
#end
#if($table.PkColumn.SqlTypeName == 'BINARY' || $table.PkColumn.SqlTypeName == 'VARBINARY' || $table.PkColumn.SqlTypeName == 'LONGVARBINARY')
#set($JavaType = "byte[]")
#end
#if($table.PkColumn.SqlTypeName == 'DATE' || $table.PkColumn.SqlTypeName == 'TIME' || $table.PkColumn.SqlTypeName == 'TIMESTAMP' || $column.SqlTypeName == 'DATETIME')
#set($JavaType = "Date")
#end
#if($table.PkColumn.SqlTypeName == 'DECIMAL' || $table.PkColumn.SqlTypeName == 'NUMERIC')
#set($JavaType = "BigDecimal")
#end

/**
 *
 * @author hfatec codegen
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Controller
@RequestMapping("/${module}/${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}")
public class ${table.destinationClassName}Action extends ShmsBaseAction {

    //服务层
    @Resource(name = "${table.destinationClassName}Service")
    private ${table.destinationClassName}Service ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Service;


    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/list")
    public String list(${table.destinationClassName}Params ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Params, Page page, Model model) {

        Table table = ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Service.query${table.destinationClassName}Table(${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Params, page);
        model.addAttribute("${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}sTable", table);
        model.addAttribute("${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Params", ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Params);
        return "/${module}/${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}/list";
    }

    /**
     *
     * 异步查询列表
     *
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(${table.destinationClassName}Params ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Params, Page page, Model model) {
        try {
            list(${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Params, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/${module}/${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到新增页
     *
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            ${table.destinationClassName} ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)} = new ${table.destinationClassName}();
            model.addAttribute("${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}", ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)});
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/${module}/${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到编辑页
     *
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            ${table.destinationClassName} ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)} = ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Service.get${table.destinationClassName}(id);
            model.addAttribute("${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}", ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)});
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/${module}/${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 根据IDs删除数据
     *
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete${table.destinationClassName}(String ${table.PkColumn.variableName}s) {
        try {
            List<String> ${table.PkColumn.variableName}List = new ArrayList<String>();
            if(${table.PkColumn.variableName}s.length() > 0) {
                ${table.PkColumn.variableName}List = Arrays.asList(${table.PkColumn.variableName}s.split(","));
            }
            ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Service.delete${table.destinationClassName}Batch(${table.PkColumn.variableName}List);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据" + ${table.PkColumn.variableName}s + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     *
     * 保存数据
     *
     */
    @RequestMapping("/save")
    @ResponseBody
    public String save${table.destinationClassName}(@Valid ${table.destinationClassName} ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}, BindingResult result) {

        try {
            if(result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            ${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)}Service.save${table.destinationClassName}(${table.destinationClassName.substring(0, 1).toLowerCase()}${table.destinationClassName.substring(1)});
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据${table.destinationClassName}保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }
}
